<?php
/**
 *author:韩帅
 *createtime:2017/6/22 9:05
 *description:
 */
namespace App\Http\Controllers\App;

use App\Models\ShopUserCenter;
use Illuminate\Http\Request;
use App\Models\UserServerBalance;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class WidthdrawSystemController extends BaseController
{
    /**
     * 发起提现
     */
    public function widthDraw(Request $request)
    {
        $card_name   = '';//存放用户的real_name;
        $user_mobile = '';//存放用户的手机号;
        $id          = session('user_id');
        $money       = $request->input('money');
        $card_id     = $request->input('card_id');
        $pay_pwd     = $request->input('pay_pwd');

        if (empty($id)  || empty($money) || empty($card_id) || empty($pay_pwd)) {

            return self::error('必填字段不能为空');
        }

        $objA = User::find($id);   //用户平台资金表

        if (empty($objA)) {

            return self::error('当前登录异常');
        }

        //提现用户要经过实名认证,有真实姓名和银行卡号
        if (empty($objA->real_name) || empty($objA->id_card)) {

            return self::error('请先完成实名认证');
        }

        //判断支付密码是否为空
        if (empty($objA->pay_pwd)) {
            return self::error('请先设定支付密码',400);
        }

        //验证支付密码
        $res = User::check_pwd($pay_pwd,$objA->pay_pwd);

        if ($res == false ) {
            return self::error('支付密码错误',308);
        }

        //提现人的真实姓名和手机号
        $card_name   = $objA->real_name;
        $user_mobile = $objA->user_mobile;

        //根据主键ID获取银行卡信息
        $info = UserServerBalance::getCard($card_id);

        //如果获取的银行卡信息为空,wd_user_card表没有这个主键ID
        if (empty($info)) {
            return self::error('银行卡ID有误',400);
        }

        //1这里比充值多一步,判断余额是否够扣
        if ($objA->balance < $money) {

            return self::error('会员余额不足',310);
        }

        $objB = new UserServerBalance();  //用户平台资金流水表

        //开事务
        $res = false;
        DB::beginTransaction();
        try{

            //2.这里再次判断余额是否够扣
            if ($objA->balance-$money < 0) {

                return self::error('会员余额不足');
            }

            $objA->balance = $objA->balance-$money;
            $objA->save();

            $objB->user_id     =     $id;                                //用户ID
            $objB->order_sn    =     '';                                 //订单号
            $objB->money       =     $money;                             //金额
            $objB->type        =     UserServerBalance::TYPE_WIDTHDRAW;  //类型为扣款
            $objB->action_type =     UserServerBalance::ACTION_TYPE_IN;  //支出
            $objB->card_num    =     $info->card_num;                    //银行卡号
            $objB->status      =     UserServerBalance::STATUS_ONE;      //状态为未审核
            $objB->card_tip    =     $info->name;                        //银行类型
            $objB->card_name   =     $card_name;                         //真实姓名
            $objB->user_mobile =     $user_mobile;                       //手机号

            $objB->save();
            // 提交事务
            DB::commit();
            $res = true;
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
        }

        if ($res == true) {

            $data = [];
            $card = mb_substr($info->card_num,-4); //银行卡后四位

            $data['money']    = $money;
            $data['type']     = '银行卡';
            $data['card_tip'] = $info->name.'(尾号'.$card.')';
            $data['message']  = '因资金清算的原因,您的提现资金将会在2-4个工资日到账,如遇节假日将顺延';
            return self::json($data,'提现成功',200);
        }
        return self::error('操作失败');
    }

    /**
     * 交易流水
     */
    public function widthdrawList(Request $request)
    {

        $return      = [];                       //存放查询出的数据
        $newArr      = [];                       //存放新数组
        $id          = session('user_id');       //用户ID;
        $type        = $request->input('type',0);
        $time        = $request->input('time',0);

        $obj         = new UserServerBalance();

        if (!empty($type)) {

            $obj = $obj->where('type',$type-1);
        }
        if($time > 0){
            $month = date('Y-m',$time);

            $obj = $obj->where('created_at','>=', date('Y-m-d H:i:s',strtotime($month)));
            $obj = $obj->where('created_at','<=', date('Y-m-d H:i:s',mktime(23, 59, 59, date('m', strtotime($month))+1, 00)));
        }

        $data   = $obj->where('user_id',$id)->orderBy('created_at','desc')->paginate(5)->toArray();

        $return = $data;

        if (empty($return['data'])) {

            $newArr['total']        = $return['total'];
            $newArr['per_page']     = $return['per_page'];
            $newArr['current_page'] = $return['current_page'];
            $newArr['last_page']    = $return['last_page'];
            $newArr['balance_log']  = [];

            return self::json($newArr,'数据为空',200);
        }

        $objB    = new UserServerBalance();
        $newArr  = $objB->translate($return);

        return self::json($newArr,'成功',200);
    }

    /**
     *余额获取
     */
    public function getInfo ()
    {
        $return  = [];

        $user_id = session('user_id');

        $info    = User::find($user_id);

        if (empty($info)) {
            return self::error('当前登录用户异常');
        }

        $return['id']      = $info->id;
        $return['balance'] = $info->balance;

        return self::json($return,'成功',200);
    }

    /**
     * 测试支付成功加日志
     */
//    public function aa () {
//        $res = UserServerBalance::insertLog(1,111,11);
//
//        if ($res == false) {
//
//            return self::error('失败');
//        }
//
//        return self::success('成功');
//    }
}